//
//  ZDDTabBar.swift
//  小蜗牛_新浪微博
//
//  Created by 周冬冬 on 16/6/25.
//  Copyright © 2016年 我的mac. All rights reserved.
//

import UIKit
//声明协议
protocol ZDDTabBarDelegate: NSObjectProtocol{

//声明协议方法
    func composeButtonClick()


}
class ZDDTabBar: UITabBar {

    weak var ZDDDelegate:ZDDTabBarDelegate?
    
   //重写父类的init方法
   override init(frame: CGRect) {
    super.init(frame:frame)
    
    setupUI()
    
    }

    func setupUI(){
    //添加
    addSubview(composeButton)
    
    
    
    }
    //设置位置
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        let width = kScreenW / 5
        //记录标识
        var index:CGFloat = 0
        
        
        //通过遍历获取每个UITabBarButton
        
        for value in self.subviews{
        
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!){
            
            //设置UITabBarButton的宽 和 位置
                value.frame.size.width = width
                
                value.frame.origin.x = width * index
                
                index++
                
                if index == 2{
                
                
                index++
                
                }
                
            
            }
        
        }
       
        composeButton.center.x = self.frame.width * 0.5
        
        composeButton.center.y = self.frame.height * 0.5
       
    }
    
 
   required init?(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }

    //撰写Button按钮
    
    private lazy var composeButton:UIButton = {
    
        
        let button = UIButton()
        
        //添加点击事件
        button.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        //设置背景颜色图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        //设置图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        button.sizeToFit()
    
    return button
    
    }()
    
    
    //点击事件
  @objc private func buttonClick(){
   //代理方法的实现
    ZDDDelegate?.composeButtonClick()
    
    }
    
}
